package com.nf.health.app.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.nf.health.app.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

public class ImageLoaderUtils {

	private static ImageLoaderUtils mImageLoaderUtil;
	private int defultImageResId = R.drawable.default_square_image;
	private int errorImageResId = R.drawable.default_square_image;
	private int emptyImageResId = R.drawable.default_square_image;
	private static ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;

	private ImageLoaderUtils(Context context) {
		imageLoader = ImageLoader.getInstance();
//		imageLoader.init(ImageLoaderConfiguration.createDefault(context));
		options = new DisplayImageOptions.Builder().showImageOnLoading(defultImageResId).showImageForEmptyUri(errorImageResId).showImageOnFail(emptyImageResId).cacheInMemory(true).cacheOnDisc(true)
				.considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).build();
	}
	
	
	

	public static ImageLoaderUtils getinstance(Context context) {
		if (mImageLoaderUtil == null) {
			synchronized (ImageLoaderUtils.class) {
				if (mImageLoaderUtil == null) {
					mImageLoaderUtil = new ImageLoaderUtils(context);
				}
			}
		}
		return mImageLoaderUtil;
	}

	/**
	 * 获取图片
	 * 
	 * @param imageView
	 * @param url
	 */
	public void getImageNoDefault(final ImageView imageView, final String url) {
		imageLoader.displayImage(url, imageView);
	}
	/**
	 * 获取图片
	 * 
	 * @param imageView
	 * @param url
	 */
	public void getImage(final ImageView imageView, final String url) {
		imageLoader.displayImage(url, imageView, options);
	}


	/**
	 * 获取图片
	 * @param imageView
	 * @param url
	 * @param defImageResId
	 */
	public void getImage(final ImageView imageView, final String url, final int defImageResId) {
		final DisplayImageOptions optionsT = new DisplayImageOptions.Builder().showImageOnLoading(defImageResId).showImageForEmptyUri(defImageResId).showImageOnFail(defImageResId).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).build();

		imageLoader.displayImage(url, imageView, optionsT);
	}


	/**
	 * 矩形圆角
	 * @param imageView
	 * @param url
	 * @param defImageResId
	 * @param cornerRadiusPixels
     */
	public void getRoundedImage(final ImageView imageView, final String url, final int defImageResId,int cornerRadiusPixels) {
		final DisplayImageOptions options = new DisplayImageOptions.Builder()
		.showImageOnLoading(defImageResId)
		.showImageForEmptyUri(defImageResId)
		.showImageOnFail(defImageResId)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.considerExifParams(true)
		.displayer(new RoundedBitmapDisplayer(cornerRadiusPixels))
		.build();
		imageLoader.displayImage(url, imageView, options);
	}

	/**
	 * 圆形图片
	 * @param imageView
	 * @param url
	 * @param defImageResId
	 */
	public void getRoundedCornerBitmap(final ImageView imageView, final String url, final int defImageResId) {
		final DisplayImageOptions options = new DisplayImageOptions.Builder()
		.showImageOnLoading(defImageResId)
		.showImageForEmptyUri(defImageResId)
		.showImageOnFail(defImageResId)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.considerExifParams(true)
		.displayer(new BitmapDisplayer() {
			
			@Override
			public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom arg2) {
				if (!(imageAware instanceof ImageViewAware)) {
					throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
				}
				imageAware.setImageBitmap(BitmapUtils.toRoundBitmap(bitmap));
			}
		})
		.build();
		imageLoader.displayImage(url, imageView, options);
	}

}
